package de.zinulla.moviethek.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class MovieDataSource {
	
	public static final int COLUMN_ID = 0;
	public static final int COLUMN_MOVIE_NAME = 1;
	public static final int COLUMN_DIRECTOR = 2;
	public static final int COLUMN_ACTOR = 3;
	public static final int COLUMN_YEAR = 4;
	public static final int COLUMN_COVER = 5;
	public static final int COLUMN_RATING = 6;
	public static final int COLUMN_PLOT = 7;
	public static final int COLUMN_GENRE = 8;
	public static final int COLUMN_RUNTIME = 9;
	public static final int COLUMN_IMDB_ID = 10;
	public static final int COLUMN_IN_3D = 11;
	public static final int COLUMN_MEDIA = 12;
	public static final int COLUMN_COVER_URL = 13;
	public static final int COLUMN_COVER_FILE_NAME = 14;
	public static final int COLUMN_RENT_NAME = 15;
	public static final int COLUMN_RENT_DATE = 16;
	// Database fields
	private SQLiteDatabase database;
	private MovieSqlHelper dbHelper;
	private String[] allColumns = { MoviesTable.COLUMN_ID, MoviesTable.COLUMN_MOVIE_NAME,
			MoviesTable.COLUMN_DIRECTOR, MoviesTable.COLUMN_ACTOR, MoviesTable.COLUMN_YEAR,
			MoviesTable.COLUMN_COVER, MoviesTable.COLUMN_RATING, MoviesTable.COLUMN_PLOT,
			MoviesTable.COLUMN_GENRE, MoviesTable.COLUMN_RUNTIME, MoviesTable.COLUMN_IMDB_ID, MoviesTable.COLUMN_IN_3D, 
			MoviesTable.COLUMN_MEDIA, MoviesTable.COLUMN_COVER_URL, MoviesTable.COLUMN_COVER_FILE_NAME,
			MoviesTable.COLUMN_RENT_NAME, MoviesTable.COLUMN_RENT_DATE};

	public MovieDataSource(Context context) {
		dbHelper = new MovieSqlHelper(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public MovieDb createMovie(MovieDb moviedb) {
		
		ContentValues values = new ContentValues();
		values.put(MoviesTable.COLUMN_MOVIE_NAME, moviedb.getMovieName());
		values.put(MoviesTable.COLUMN_DIRECTOR, moviedb.getDirector());
		values.put(MoviesTable.COLUMN_ACTOR, moviedb.getActor());
		values.put(MoviesTable.COLUMN_YEAR, moviedb.getYear());
		values.put(MoviesTable.COLUMN_COVER, moviedb.getCover());
		values.put(MoviesTable.COLUMN_RATING, moviedb.getRating());
		values.put(MoviesTable.COLUMN_PLOT, moviedb.getPlot());
		values.put(MoviesTable.COLUMN_GENRE, moviedb.getGenre());
		values.put(MoviesTable.COLUMN_RUNTIME, moviedb.getRuntime());
		values.put(MoviesTable.COLUMN_IMDB_ID, moviedb.getImdbId());
		values.put(MoviesTable.COLUMN_IN_3D, moviedb.getIn3d());
		values.put(MoviesTable.COLUMN_MEDIA, moviedb.getMedia());
		values.put(MoviesTable.COLUMN_COVER_URL, moviedb.getCoverUrl());
		values.put(MoviesTable.COLUMN_COVER_FILE_NAME, moviedb.getCoverFileName());
		values.put(MoviesTable.COLUMN_RENT_NAME, moviedb.getRentName());
		values.put(MoviesTable.COLUMN_RENT_DATE, moviedb.getRentDate());
		
		long insertId = database.insert(MoviesTable.TABLE_MOVIES, null, values);
		Cursor cursor = database.query(MoviesTable.TABLE_MOVIES, allColumns, MoviesTable.COLUMN_ID + " = "
				+ insertId, null, null, null, null);
		cursor.moveToFirst();
		MovieDb newMovie = cursorToMovie(cursor);
		cursor.close();
		return newMovie;
	}

	public void deleteComment(MovieDb movie) {
		long id = movie.getId();
		System.out.println("Comment deleted with id: " + id);
		database.delete(MoviesTable.TABLE_MOVIES, MoviesTable.COLUMN_ID + " = " + id, null);
	}

	public List<MovieDb> getAllMovies() {
		List<MovieDb> allMovies = new ArrayList<MovieDb>();

		Cursor cursor = database.query(MoviesTable.TABLE_MOVIES, allColumns, null, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			MovieDb movie = cursorToMovie(cursor);
			allMovies.add(movie);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return allMovies;
	}

	/**
	 * liest einen einzelnen Film anhand der SQL ID
	 * @return
	 */
	public MovieDb getMovieBySqlId(long id) {
		
		Cursor cursor = database.query(MoviesTable.TABLE_MOVIES, allColumns, MoviesTable.COLUMN_ID + "=" + id, null, null, null, null);
		
		cursor.moveToFirst();
		MovieDb movie = null;
		while (!cursor.isAfterLast()) {
			movie = cursorToMovie(cursor);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return movie;
	}

	public Cursor getAllMoviesCursor() {
		
		Cursor cursor = database.query(MoviesTable.TABLE_MOVIES, allColumns, null, null, null, null, null);
		
		// Make sure to close the cursor
		cursor.close();
		return cursor;
	}

	private MovieDb cursorToMovie(Cursor cursor) {
		MovieDb movie = new MovieDb();
		movie.setId(cursor.getLong(COLUMN_MOVIE_NAME));
		movie.setMovieName(cursor.getString(COLUMN_MOVIE_NAME));
		movie.setDirector(cursor.getString(COLUMN_DIRECTOR));
		movie.setActor(cursor.getString(COLUMN_ACTOR));
		movie.setYear(cursor.getString(COLUMN_YEAR));
		movie.setCover(cursor.getBlob(COLUMN_COVER));
		movie.setRating(cursor.getString(COLUMN_RATING));
		movie.setPlot(cursor.getString(COLUMN_PLOT));
		movie.setGenre(cursor.getString(COLUMN_GENRE));
		movie.setRuntime(cursor.getString(COLUMN_RUNTIME));
		movie.setImdbId(cursor.getString(COLUMN_IMDB_ID));
		movie.setIn3d(cursor.getString(COLUMN_IN_3D));
		movie.setMedia(cursor.getString(COLUMN_MEDIA));
		movie.setCoverUrl(cursor.getString(COLUMN_COVER_URL));
		movie.setCoverFileName(cursor.getString(COLUMN_COVER_FILE_NAME));
		movie.setRentName(cursor.getString(COLUMN_RENT_NAME));
		movie.setRentDate(cursor.getString(COLUMN_RENT_DATE));
		return movie;
	}
}
